package com.genghis.ptas.note.service.impl;

import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.note.dao.NoteDao;
import com.genghis.ptas.note.entity.Note;
import com.genghis.ptas.note.entity.PublicityNote;
import com.genghis.ptas.note.service.NoteService;
import com.genghis.ptas.note.service.PublicityNoteService;
import com.genghis.ptas.security.dao.UserDao;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.util.DateUtil;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Service("noteService")
public class NoteServiceImpl implements NoteService {

    @Autowired
    private NoteDao noteDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private DictService dictService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private PublicityNoteService publicityNoteService;

    @Override
    public int addNote(Note note) {
        note.setCreateDate(DateUtil.getSystemDate(DictConstant.DATE_FORMAT));
        note.setUserId(userDao.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId());
        return noteDao.addNote(note);
    }

    @Override
    public void editNote(Note note) {
        noteDao.editNote(note);
    }

    @Override
    public void delNote(String noteId) {
        noteDao.delNote(noteId);
    }

    @Override
    public Note findNoteById(int noteId) {
        Note note = noteDao.findNoteById(noteId);
        String userName;
        try {
            userName = userDao.findUserAllInfoById(note.getUserId()).getUserName();
        } catch (Exception e) {
            userName = "未知";
        }
        note.setUserName(userName);
        note.setShowNoteType(classifyService.findTypeValuesById(note.getNoteType()).getTypeValue());
        return note;
    }

    @Override
    public List<Note> findNoteByType(Note note) {
        List<Note> noteList = noteDao.findNoteByType(note);
        for (Note noteTemp : noteList) {
            TypeValues typeValue = classifyService.findTypeValuesById(noteTemp.getNoteType());
            noteTemp.setShowNoteType(typeValue.getTypeValue());
            noteTemp.setCreateDate(noteTemp.getCreateDate().substring(0, 19));
            if (noteTemp.getState() == DictConstant.MINUS_ONE) {
                noteTemp.setShowState("未申请公示");
            } else if (noteTemp.getState() == DictConstant.DATE_INT) {
                noteTemp.setShowState("未审核");
            } else {
                noteTemp.setShowState(dictService.changeDictCodeToValue(noteTemp.getState(), DictConstant.AUDIT_STATE));
            }
            noteTemp.setShowColor(getColorMap().get(noteTemp.getNoteType()));
        }
        return noteList;
    }

    private Map<Integer, String> getColorMap() {
        Map<Integer, String> map = new HashMap();
        List<String> colorList = new ArrayList<String>();
        colorList.add("yellow");
        colorList.add("blue");
        colorList.add("green");
        colorList.add("purple");
        colorList.add("red");
        colorList.add("grey");
        colorList.add("blue");
        colorList.add("black");
        colorList.add("white");
        List<TypeValues> typeValuesList = classifyService.findAllTypeValue();
        int i = 0, len = colorList.size();
        for (TypeValues typeValues : typeValuesList) {
            map.put(typeValues.getId(), colorList.get(i % len));
            i++;
        }
        return map;
    }

    @Override
    public void publicNotes(String ids) {
        String[] idList = ids.split(",");

        for (String id : idList) {
            publicityNoteService.delPublicityNote(id);
            PublicityNote publicityNote = new PublicityNote();
            publicityNote.setNoteId(Integer.parseInt(id));
            publicityNoteService.addPublicityNote(publicityNote);
        }
    }

    @Override
    public List<TypeValues> findAllNote(Note note) {
        List<TypeValues> typeValuesList = classifyService.findAllTypeValue();
        for (TypeValues typeValues : typeValuesList) {
            note.setNoteType(typeValues.getId());
            typeValues.setNotes(findNoteByType(note));
        }
        return typeValuesList;
    }

    @Override
    public Note findNoteByCourseIdAndUserId(Integer courseId) {
        Integer userId = userDao.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        return noteDao.findNoteByCourseIdAndUserId(courseId, userId);
    }

    @Override
    public List<TypeValues> findPublicNote(Note note) {
        List<TypeValues> typeValuesList = classifyService.findAllTypeValue();
        for (TypeValues typeValues : typeValuesList) {
            note.setNoteType(typeValues.getId());
            typeValues.setNotes(findPublicNoteDo(note));
        }
        return typeValuesList;
    }

    @Override
    public List<Note> findAllPublicNote(Note note) {
        return noteDao.findPublicNote(note);
    }

    private List<Note> findPublicNoteDo(Note note) {
        List<Note> noteList = noteDao.findPublicNote(note);
        for (Note noteTemp : noteList) {
            TypeValues typeValue = classifyService.findTypeValuesById(noteTemp.getNoteType());
            noteTemp.setShowNoteType(typeValue.getTypeValue());
            if (noteTemp.getState() == DictConstant.MINUS_ONE) {
                noteTemp.setShowState("未申请公示");
            } else if (noteTemp.getState() == DictConstant.DATE_INT) {
                noteTemp.setShowState("未审核");
            } else {
                noteTemp.setShowState(dictService.changeDictCodeToValue(noteTemp.getState(), DictConstant.AUDIT_STATE));
            }
        }
        return noteList;
    }
}
